import React, { useState, useEffect } from 'react'
import { Col, Form, Input, Button, Table } from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import RenderButtom from '@/components/RenderButton'
import SearchBox from '@/components/SearchBox'
import { getCardList } from '@/services/supplier/withdraw/cardList'

// 表头
const columns = [
  {
    title: '开户行',
    dataIndex: 'bank',
    key: 'bank',
    align: 'center'
  },
  {
    title: '开户行地址',
    dataIndex: 'address',
    key: 'address',
    align: 'center'
  },
  {
    title: '卡类型',
    dataIndex: 'type',
    key: 'type',
    align: 'center'
  },
  {
    title: '卡号',
    dataIndex: 'cardNo',
    key: 'cardNo',
    align: 'center'
  },
  {
    title: '所属人',
    dataIndex: 'master',
    key: 'master',
    align: 'center'
  },
  {
    titlel: '身份证号',
    dataIndex: 'pid',
    key: 'pid',
    align: 'center'
  },
  {
    titlel: '所属门店',
    dataIndex: 'store',
    key: 'store',
    align: 'center'
  }
]

const rowSelection = {
  type: 'radio',
  onChange: (selectedRowKeys, selectedRows) => {
    setRecord(selectedRows)
    // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    console.log(record)
  },
}

const CardList = props => {
  const { form } = props
  const { getFieldDecorator } = form
  const [cardList, setCardList] = useState([])
  const [total, setTotal] = useState(1)
  const [pageNo, setPageNo] = useState(1)
  const [pageSize, setPageSize] = useState(10)
  const [record, setRecord] = useState({})

  useEffect(() => {
    init()
  }, [])
  const init = async () => {
    await getCardLists()
  }
  // 表单查询
  const handleSearch = e => {
    e.preventDefault()
    form.validateFields((err, values) => {
      console.log('Received values of form: ', values)

    })
  }
  // 表单清空
  const handleReset = e => {
    form.resetFields();
  }
  // 查询条件
  const getFields = () => {
    return (
      <Col span={8}>
        <Form.Item label={`名称`}>
          {getFieldDecorator(`id`, {})
            (<Input />)}
        </Form.Item>
      </Col>
    )
  }
  // 按钮处理
  const handleBtn = type => {

  }
  // 获取银行卡列表
  const getCardLists = async (params) => {
    let res = await getCardList(params)
    if (res.code === '0') {
      setCardList(res.data)
      setTotal(res.iTotalDisplayRecords)
    } else {
      alert(res.msg)
    }
  }

  const pagination = {
    total: parseInt(total),
    onChange(page) {
      let params = {
        pageNo: page,
        pageSize
      }
      getCardLists(params)
    }
  }
  return (
    <PageHeaderWrapper>
      <SearchBox handleReset={handleReset} handleSearch={handleSearch} getFields={getFields} />
      <RenderButtom click={handleBtn} />
      <Table
        columns={columns}
        dataSource={cardList}
        rowKey={record => record.id}
        pagination={pagination}
        rowSelection={rowSelection}
      />
   </PageHeaderWrapper>
  )
}

export default Form.create()(CardList)
